<template>
  <a-modal
    :visible.sync="visible"
    :maskClosable="false"
    :destroyOnClose="true"
    title="管理群组"
    width="862px"
    :footer="null"
    wrapClassName="manage-group-modal-container"
    @cancel="_handleCancel()"
  >
    <!--  -->
    <a-form-model ref="ruleForm" :model="ruleForm" :rules="rules">
      <a-form-model-item ref="isOpen" label="是否开启" prop="isOpen">
        <a-radio-group v-model="ruleForm.isOpen">
          <a-radio value="1"> 是 </a-radio>
          <a-radio value="0"> 否 </a-radio>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item
        ref="isOpen"
        label="选择群组"
        v-if="ruleForm.isOpen == '1'"
      >
        <div class="transfer-container flex">
          <!-- 未选群组 -->
          <div class="left-transfer flex" v-loading="pageLoading">
            <div class="title flex">
              <div class="text">未选群组</div>
              <a-checkbox @change="allSelect($event)"> 全选 </a-checkbox>
            </div>
            <div class="container">
              <div class="condation">
                <a-input
                  placeholder="请输入群组名称"
                  v-model="groupName"
                  @pressEnter="filterData()"
                />
                <div class="search-button" @click="filterData()">
                  <bj-icon-font
                    type="search"
                    :size="20"
                    color="#8A8A8C"
                  ></bj-icon-font>
                </div>
              </div>
              <div class="data-container">
                <a-empty
                  :image="simpleImage"
                  v-if="noChoosedList.length == 0"
                />
                <a-checkbox-group
                  v-model="choosedKeys"
                  v-if="noChoosedList.length > 0"
                >
                  <a-row>
                    <a-col
                      :span="24"
                      v-for="item in noChoosedList"
                      :key="item.id"
                    >
                      <a-checkbox :value="item.id">
                        {{ item.groupName }}
                      </a-checkbox>
                    </a-col>
                  </a-row>
                </a-checkbox-group>
              </div>
            </div>
          </div>
          <!--  -->
          <div class="center-button flex">
            <div
              class="choose-button"
              :class="choosedKeys.length == 0 && 'dis'"
              @click="transferData()"
            ></div>
          </div>
          <!-- 已选群组 -->
          <div class="right-transfer flex">
            <div class="title flex">
              <div class="text">已选群组</div>
              <div class="all-clear" @click="allClear()">全部清空</div>
            </div>
            <div class="container">
              <div
                class="has-choosed-item"
                v-for="item in hasChoosedList"
                :key="item.id"
              >
                {{ item.groupName }}
                <div class="close" @click="removeChoosed(item)">
                  <bj-icon-font
                    type="close"
                    :size="12"
                    color="#fff"
                  ></bj-icon-font>
                </div>
              </div>
              <a-empty :image="simpleImage" v-if="hasChoosedList.length == 0" />
            </div>
          </div>
        </div>
      </a-form-model-item>
    </a-form-model>
    <!-- 底部 -->
    <div class="modal-footer flex">
      <a-button @click="_handleCancel()"> 取消 </a-button>
      <a-button
        type="primary"
        style="margin-left: 10px"
        v-loading="submitLoading"
        @click="onSubmit()"
      >
        确定
      </a-button>
    </div>
  </a-modal>
</template>

<script>
import {
  queryGroup,
  saveSpecialNotice,
} from "@/api/user-center/equipmentManage.js";
import { Empty } from "ant-design-vue";
import _ from "lodash";
export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    equNum: {
      type: [Boolean, String],
      default: "",
    },
    isOpen: {
      type: [Boolean, String],
      default: "1",
    },
    groupIds: {
      type: String,
      default: "",
    },
    noticeType: {
      type: [Boolean, String],
      default: "",
    },
  },
  data() {
    return {
      groupName: undefined,
      simpleImage: undefined,
      pageLoading: false,
      submitLoading: false,
      ruleForm: {
        isOpen: "1",
      },
      rules: {
        isOpen: [
          { required: true, message: "请选择是否开启", trigger: "change" },
        ],
      },
      noChoosedList: [],
      hasChoosedList: [],
      choosedKeys: [],
      orignData: [],
    };
  },
  watch: {
    visible: {
      handler(val) {
        val && this.queryGroup();
      },
      immediate: true,
    },
    isOpen: {
      handler(val) {
        this.ruleForm.isOpen = val;
      },
      immediate: true,
    },
  },
  beforeCreate() {
    this.simpleImage = Empty.PRESENTED_IMAGE_SIMPLE;
  },
  methods: {
    //提交
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let { equNum, ruleForm, noticeType, hasChoosedList } = this;
          let groupIds = hasChoosedList.map((v) => v.id).toString();
          if (this.ruleForm.isOpen == "1") {
            if (groupIds.length == 0) {
              this.$message.warn("请至少选择一个群组");
              return;
            }
          }
          this.submitLoading = true;
          saveSpecialNotice({
            equNum,
            isOpen: ruleForm.isOpen,
            noticeType,
            groupIds: ruleForm.isOpen == "1" ? groupIds : "",
          })
            .then((res) => {
              this.$message.success("操作成功");
              this._handleCancel();
              this.$parent.querynotificationList();
            })
            .catch((_) => {})
            .finally((_) => {
              this.submitLoading = false;
            });
        } else {
          return false;
        }
      });
    },

    //搜索
    filterData() {
      let { groupName, orignData } = this;
      this.noChoosedList = orignData.filter((v) =>
        v.groupName.toLowerCase().includes(groupName.toLowerCase())
      );
    },

    //未选群组-全选
    allSelect(ev) {
      let { target } = ev;
      if (target?.checked) {
        this.choosedKeys = this.noChoosedList.map((v) => v.id);
      } else {
        this.choosedKeys = [];
      }
    },

    //单个删除已选群组
    removeChoosed(item) {
      let { id } = item;
      this.hasChoosedList = this.hasChoosedList.filter((v) => v.id != id);
      this.noChoosedList.push(item);
      this.orignData = _.cloneDeep(this.noChoosedList);
    },

    //已选群组-全部清空
    allClear() {
      this.noChoosedList = this.noChoosedList.concat(this.hasChoosedList);
      this.hasChoosedList = [];
    },

    //查询群组列表
    queryGroup() {
      this.pageLoading = true;
      let { equNum, groupIds } = this;
      queryGroup({ equNum })
        .then((res) => {
          let ids = groupIds.split(",");
          let { data } = res;
          this.noChoosedList = (data?.result || []).filter(
            (v) => !ids.includes(v.id)
          );
          this.hasChoosedList = (data?.result || []).filter((v) =>
            ids.includes(v.id)
          );
          this.orignData = _.cloneDeep(this.noChoosedList);
        })
        .catch((_) => {
          this.noChoosedList = [];
          this.orignData = [];
        })
        .finally((_) => {
          this.pageLoading = false;
        });
    },

    //移动数据到右侧
    transferData() {
      let { choosedKeys } = this;
      if (choosedKeys.length == 0) return;
      let items = this.noChoosedList.filter((v) => choosedKeys.includes(v.id));
      this.noChoosedList = this.noChoosedList.filter(
        (v) => !choosedKeys.includes(v.id)
      );
      this.choosedKeys = [];
      this.hasChoosedList = this.hasChoosedList.concat(items);
      this.orignData = _.cloneDeep(this.noChoosedList);
    },

    _handleCancel() {
      this.$emit("update:visible", false);
      this.resetData();
    },

    //
    resetData() {
      this.noChoosedList = [];
      this.hasChoosedList = [];
      this.orignData = [];
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.manage-group-modal-container {
  /deep/.ant-form {
    .ant-form-item {
      .flex();
      .ant-form-item-label {
        width: 100px;
        flex-shrink: 0;
      }
      .ant-form-item-control-wrapper {
        flex: 1;
        textarea {
          height: 80px;
        }
        .ant-calendar-picker {
          width: 100%;
        }
      }
    }
  }
  .transfer-container {
    height: 424px;
    .left-transfer,
    .right-transfer {
      height: 100%;
      width: 300px;
      border: 1px solid #d4d7d8;
      border-radius: 4px 4px 4px 0px;
      flex-direction: column;
      overflow: hidden;
      .title {
        flex-shrink: 0;
        height: 36px;
        background: #f3f4f6;
        justify-content: space-between;
        align-items: center;
        padding: 0 4px 0 10px;
        .text {
          font-size: 14px;
          font-family: Microsoft YaHei;
          font-weight: bold;
          color: #2b2c30;
        }
      }
      .container {
        flex: 1;
      }
    }
    .left-transfer {
      .container {
        overflow: hidden;
        .flex();
        flex-direction: column;
        .condation {
          padding: 10px;
          position: relative;
          flex-shrink: 0;
          .search-button {
            position: absolute;
            top: 16px;
            right: 12px;
            height: 32px;
            width: 30px;
            background: #fff;
            .flex();
            justify-content: center;
            align-items: center;
            cursor: pointer;
          }
        }
        .data-container {
          flex: 1;
          padding: 0 10px 8px 10px;
          .custom-scrollbar();
          /deep/.ant-checkbox-group {
            width: 100%;
            .ant-col {
              margin-bottom: 12px;
            }
            .ant-checkbox-wrapper {
              width: 100%;
              &.ant-checkbox-wrapper-checked {
                color: var(--primary-color);
              }
            }
          }
        }
      }
    }
    .center-button {
      height: 100%;
      width: 66px;
      justify-content: center;
      align-items: center;
      .choose-button {
        width: 46px;
        height: 28px;
        background: var(--primary-color)
          url("https://jszhgd.cn/static/pc/transfer.png") no-repeat center
          center;
        border-radius: 4px;
        cursor: pointer;
        &:active {
          opacity: 0.8;
        }
        &.dis {
          background-color: #dcdcdc;
          cursor: not-allowed;
          &:active {
            opacity: 1;
          }
        }
      }
    }
    .right-transfer {
      .title {
        padding-right: 10px;
        .all-clear {
          color: var(--primary-color);
          cursor: pointer;
        }
      }
      .container {
        padding: 10px 10px 10px 10px;
        .custom-scrollbar();
        .has-choosed-item {
          float: left;
          background: #f1f2f7;
          border-radius: 4px;
          align-items: center;
          line-height: normal;
          .flex();
          flex-shrink: 0;
          color: var(--primary-color);
          margin-right: 10px;
          margin-bottom: 10px;
          padding: 8px 40px 8px 10px;
          position: relative;
          .close {
            position: absolute;
            width: 15px;
            height: 15px;
            background: var(--primary-color);
            .flex();
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            top: 50%;
            transform: translateY(-50%);
            right: 10px;
            cursor: pointer;
            &:active {
              opacity: 0.8;
            }
          }
        }
      }
    }
  }
  .modal-footer {
    justify-content: center;
    align-items: center;
    padding: 20px 0 10px 0;
  }
}
</style>
